package com.atguigu.gmall.product.service.impl;

import com.atguigu.gmall.product.entity.BaseAttrValue;
import com.atguigu.gmall.product.service.BaseAttrValueService;
import com.atguigu.gmall.product.vo.SaveAttrInfoVo;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.gmall.product.entity.BaseAttrInfo;
import com.atguigu.gmall.product.service.BaseAttrInfoService;
import com.atguigu.gmall.product.mapper.BaseAttrInfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author lyd
 * @description 针对表【base_attr_info(属性表)】的数据库操作Service实现
 * @createDate 2023-08-15 13:49:56
 */
@Service
public class BaseAttrInfoServiceImpl extends ServiceImpl<BaseAttrInfoMapper, BaseAttrInfo>
        implements BaseAttrInfoService {

    @Autowired
    private BaseAttrValueService baseAttrValueService;

    @Override
    public List<BaseAttrInfo> getAttrInfoList(Long c1Id, Long c2Id, Long c3Id) {
        return baseMapper.getAttrInfoList(c1Id, c2Id, c3Id);
    }

    @Transactional
    @Override
    public void saveAttrInfo(SaveAttrInfoVo vo) {
        //1.先向base_attr_into中添加数据
        BaseAttrInfo baseAttrInfo = new BaseAttrInfo();
        //1.1复制具有相同的属性值
        BeanUtils.copyProperties(vo, baseAttrInfo);
        //1.2保存数据
        this.save(baseAttrInfo);

        //2.向base_attr_value中添加数据

        //2.1通过步骤1.2获取attrId
        Long attrId = baseAttrInfo.getId();
        //2.2通过stream将AttrValueListDTO转换为BaseAttrValue，封装在集合里面
        // List<SaveAttrInfoVo.AttrValueListDTO> attrValueList = vo.getAttrValueList();
        List<BaseAttrValue> list = vo.getAttrValueList().stream().map(
                item -> {
                    BaseAttrValue baseAttrValue = new BaseAttrValue();
                    baseAttrValue.setValueName(item.getValueName());
                    baseAttrValue.setAttrId(attrId);
                    return baseAttrValue;
                }
        ).collect(Collectors.toList());

        //2.3保存数据
        baseAttrValueService.saveBatch(list);
    }

    @Override
    public void updateAttrInfo(SaveAttrInfoVo vo) {
        //1.修改base_attr_into中的数据
        this.lambdaUpdate().set(BaseAttrInfo::getAttrName, vo.getAttrName())
                .set(BaseAttrInfo::getCategoryId, vo.getCategoryId())
                .set(BaseAttrInfo::getCategoryLevel, vo.getCategoryLevel())
                .eq(BaseAttrInfo::getId, vo.getId())
                .update();
        //2、前端返回的base_attr_value中的数据有删除，也有新增。为此，可以通过id来区分
        //notin 计算差集： delete from base_attr_value where id not in(60,61) and attr_id=13
        // 先做删除；
        // 不先做删除； 会导致把新值的也删除了

        //2.1获取前端返回的id集合
        List<Long> idList = vo.getAttrValueList().stream()
                .filter(item -> item.getId() != null)
                .map(item -> item.getId())
                .collect(Collectors.toList());
        //2.2删除前端之前有，现在没有的id的对象
        if (idList != null && idList.size() > 0) {
            baseAttrValueService.lambdaUpdate()
                    .notIn(BaseAttrValue::getId,idList)
                    .eq(BaseAttrValue::getAttrId,vo.getId())
                    .remove();
        }else {
            //2.3前端没有传任何的id过来，即全删完了
            baseAttrValueService.lambdaUpdate()
                    .eq(BaseAttrValue::getAttrId,vo.getId())
                    .remove();
        }


        vo.getAttrValueList().stream().forEach(item->{
            if (item.getId() == null) {
                //2.3没有id,说明是新增，插入新增的id的对象
                BaseAttrValue baseAttrValue = new BaseAttrValue();
                baseAttrValue.setAttrId(vo.getId());
                baseAttrValue.setValueName(item.getValueName());
                baseAttrValueService.save(baseAttrValue);
            }else {
                //2.4有id,说明是修改
               baseAttrValueService.lambdaUpdate()
                       .set(BaseAttrValue::getValueName,item.getValueName())
                       .eq(BaseAttrValue::getId,item.getId())
                       .update();
            }
        });


    }
}




